package org.goodsl.deploymap

/**
 * Created with IntelliJ IDEA.
 * User: Simon Xiao
 * Date: 12-12-19
 * Time: 上午9:58
 * Copyright goodsl.org 2012~2020
 */
class Module extends SysElement{

    boolean isIntroduced = false
    ArrayList<ModuleRelation> relations = new ArrayList<ModuleRelation>()


    void introduced(boolean isIntroduced){
        this.isIntroduced = isIntroduced
    }

    void using(Closure cl){
        ModuleRelation delegate = new ModuleRelation(
                module: this,
                relationType:ModuleRelationType.Use)
        cl.delegate = delegate
        cl.resolveStrategy = Closure.DELEGATE_FIRST
        cl()
    }

    void extend(Closure cl){

        ModuleRelation delegate = new ModuleRelation(
                module: this,
                relationType:ModuleRelationType.Ext)
        cl.delegate = delegate
        cl.resolveStrategy = Closure.DELEGATE_FIRST
        cl()

    }
    def propertyMissing(String name){
        if (name == "introduced")
            this.isIntroduced = true

    }

}

enum ModuleRelationType {
    Use,
    Ext
}


class ModuleRelation{
    AppSystem system
    String nameA
    String nameB
    ModuleRelationType relationType
    String desc =""

    Module module

    public Module getModuleA(){
        return  system.getModule(nameA)
    }
    public Module getModuleB(){
        return  system.getModule(nameB)
    }


    def methodMissing(String methodName,args){
        String description = relationType.toString()
        if (args!=null)
            description = args[0].toString()

        ModuleRelation relation = new ModuleRelation(
                system: module.system,
                nameA: module.name,
                nameB: methodName,
                relationType: this.relationType,
                desc:description
        )
        module.relations.add(relation);
    }

    def propertyMissing(String methodName){
        String description = relationType.toString()
        ModuleRelation relation = new ModuleRelation(
                system: module.system,
                nameA: module.name,
                nameB: methodName,
                relationType: this.relationType,
                desc:description
        )
        module.relations.add(relation);
    }



}